#pragma once

#include <map>
#include <string>

#include <refbase.h>
#include <singleton_delegator.h>

// 这个接口定义脱离 ServiceRegistry 就没有意义了，所以没有自己的头文件
class IService : public RefBase {
public:
    virtual ~IService() = default;

    virtual int GetID() = 0;
    virtual std::string Process(const std::string &message) = 0;
};

class ServiceRegistry : public RefBase {
public:
    static const sptr<ServiceRegistry> &GetInstance();

    // for client
    virtual void AddService(const sptr<IService> &service);
    virtual void RemoveService(const sptr<IService> &service);

    // for server
    virtual std::string DispatchMessage(int id, const std::string &message);

private:
    ServiceRegistry() = default;
    virtual ~ServiceRegistry() = default;
    static inline sptr<ServiceRegistry> instance = nullptr;
    static inline SingletonDelegator<ServiceRegistry> delegator;

    std::map<int, sptr<IService>> services;
};
